#-*- coding:utf8 -*-
"""
Function attributes:
* load: boolean. Optional. If False the function will not load.
* name: string. Optional.
* priority: string. Optional. Three values are available: "high", "medium" or "low",
  default value: "medium".
* thread: boolean. Optional. If True, function will work by starting a thread.
* rule: string or regexp object. Required.
* aliases: list of string or regexp objects. Optional.
* fullmsg: boolean. Optional. If True, pattern is searched on whole message, else
  pattern is a command, the message must begin with a sign.

* access
* evttypes: must be tuple
* limit
* channels
* url
* showas
"""

import re
from util.functions import *
from core.optimizers import fmt, File
from util.xplor import Xplorer
from util.pywikilib import WikiLink, Site, Page

field = "watchlist"

def watch(bot, evt, cmd):
	""".watch <site> {:read|:add|:new|:rem} [-list: <element1>; <element2>; ...]"""
	logname = cmd.arguments
	if logname:
		site = WikiLink(logname).site()
		_watchlist = File("watch-%s.log" % site.abredged_sitename())
		exists = _watchlist.exists()
		if exists and cmd.action == "read":
			watchlist = _watchlist.get()
			bot.priv_notice("%i elements found: %s", len(watchlist), join_list(watchlist, evt.lang))
		elif not exists and cmd.action == "read":
			bot.priv_notice("%s: file not found.", _watchlist.filename())
		elif exists and cmd.action == "add":
			if isinstance(cmd.opts['list'], basestring): cmd.opts['list'] = [cmd.opts['list']]
			_watchlist.put(cmd.opts['list'])
			bot.priv_notice("Elements were added successfully.")
		elif exists and cmd.action == "rem":
			wlist = _watchlist.get()
			if isinstance(cmd.opts['list'], basestring): cmd.opts['list'] = [cmd.opts['list']]
			for e in cmd.opts['list']: wlist.remove(e)
			_watchlist.put(wlist, clean=True)
			bot.priv_notice("Element were removed successfully.")
		else:
			bot.reply(evt, "list expected.")
watch.rule = "watch"
watch.level = "support"
